package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSON;
import com.bdqn.tour.pojo.HzyInvoice;
import com.bdqn.tour.pojo.HzyPageInfo;
import com.bdqn.tour.service.impl.HzyInvoiceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/getInvoice")
public class GetInvoiceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        // 分页
        int pageNo = 1;
        int pageSize = 5;
        if (req.getParameter("pageNo") != null) {
            pageNo = Integer.parseInt(req.getParameter("pageNo"));
        }
        if (req.getParameter("pageSize") != null) {
            pageSize = Integer.parseInt(req.getParameter("pageSize"));
        }
        // 获取分页数据
        HzyPageInfo<HzyInvoice> pageInfo = new HzyInvoiceServiceImpl().getInvoice(pageNo, pageSize);

        // 返回JSON数据
        PrintWriter out = resp.getWriter();
        String json = JSON.toJSONString(pageInfo);
        out.print(json);
        out.flush();
        out.close();
    }
}
